package sma.gps.fich;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.apache.log4j.Logger;

import sma.FilterMp;

public class PolishEntity {

	String mFamily;
	List mLstLines = new ArrayList();

	/** Log4J */
	private static final Logger sLog = Logger.getLogger(PolishEntity.class);

	public String getFamily() {
		return mFamily;
	}

	public void setFamily(String family) {
		if ("RGN80".equals(family)) {
			this.mFamily = "POLYGON";
		} else if ("RGN40".equals(family)) {
			this.mFamily = "POLYLINE";
		} else if ("RGN10".equals(family)) {
			this.mFamily = "POI";
		} else if ("RGN20".equals(family)) {
			this.mFamily = "POI";
		} else {
			this.mFamily = family;
		}
	}
	
	public void addLine(String pLine) {
		mLstLines.add(pLine);
	}

	/**
	 * @return List of String
	 */
	public List getLstLines() {
		return mLstLines;
	}
	
	public String getValueForKey (String pKey) {
		String begin = pKey + "=";
		for (Iterator iter = mLstLines.iterator(); iter.hasNext();) {
			String line = (String) iter.next();
			if (line.startsWith(begin)) {
				return line.substring(begin.length(), line.length());
			}
			
		}
		return null;
	}
	
	/**
	 * @param pKey at to set
	 * @param pValue to set
	 */
	public String setValueForKey (String pKey, String pValue) {
		String begin = pKey + "=";
		boolean found = false;
		String line = null;
		for (ListIterator iter = mLstLines.listIterator(); iter.hasNext();) {
			line = (String) iter.next();
			if (line.startsWith(begin)) {
				iter.set(pKey + "=" + pValue);
				found = true;
				break;
			}
			
		}
		if (!found) {
			sLog.warn("key "+ pKey +" not found !");
			return null;
		}
		return line;
	}
	
	public String removeKey (String pKey) {
		String begin = pKey + "=";
		//boolean found = false;
		String line = null;
		for (ListIterator iter = mLstLines.listIterator(); iter.hasNext();) {
			line = (String) iter.next();
			if (line.startsWith(begin)) {
				iter.remove();
				//found = true;
				break;
			}
			
		}
		return line;
	}
	
	public void addValueForKey (String pKey, String pValue) {
		this.addLine(pKey + "=" + pValue);
	}

}
